package com.qt.socket.tcpwork.work3;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.UUID;

/**
 * @Author zsf
 * @Date 2023/10/5 17:05
 * @Version 1.0
 */
public class ServerDemo {

    /**
     * /客户端:将本地文件上传到服务器。接收服务器的反馈。
     * //服务器:接收客户端上传的文件,上传完毕之后给出反馈。
     */
    public static void main(String[] args) throws IOException {

        ServerSocket serverSocket = new ServerSocket(8888);
        Socket socket = serverSocket.accept();

        BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());

        String name = UUID.randomUUID().toString().replaceAll("-", "");
        //写入文件
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("bbb\\" + name + ".txt"));

        byte[] bytes = new byte[1024];
        int len;
        while ((len = bis.read(bytes)) != -1) {
            bos.write(bytes, 0, len);
        }
        bos.close();

        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        bw.write("上传成功");
        bw.flush();

        socket.close();

    }

}
